package cn.itsource.order.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.LoginContext;
import cn.itsource.basic.util.PageList;
import cn.itsource.order.domain.OrderPetAcquisition;
import cn.itsource.order.dto.OrderDto;
import cn.itsource.order.mapper.OrderPetAcquisitionMapper;
import cn.itsource.order.query.OrderPetAcquisitionQuery;
import cn.itsource.order.service.IOrderPetAcquisitionService;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private PetMapper petMapper;

    @Override
    @Transactional
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //获取员工对象
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        //通过员工对象获取店铺id
        Long shopId = employee.getShopId();
        //如果店铺id不为空的话就是店铺人员
        if(shopId != null){
            //通过shopId查询出一个shop对象
            Shop shop = shopMapper.findById(shopId);
            //如果店铺的管理员id不等于员工的id，那就是店员
            if (shop.getAdminId() != employee.getId()){
                query.setEmpId(employee.getId());
            }
            query.setShopId(shopId);
        }
        return super.queryPage(query);
    }

    @Transactional
    @Override
    public void confirm(OrderDto orderDto) {
        //1.改订单状态 0审核通过待处理 - 1已处理
        OrderPetAcquisition order = orderPetAcquisitionMapper.findById(orderDto.getId());
        order.setState(1);
        //2.修改订单的支付方式和金额
        order.setPaytype(orderDto.getPayType());
        order.setPrice(orderDto.getMoney());
        orderPetAcquisitionMapper.update(order);

        //3.改寻主消息状态 1接单成功 -2已完成
        SearchMasterMsg msg = searchMasterMsgMapper.findById(order.getSearchMasterMsgId());
        msg.setState(2);
        searchMasterMsgMapper.update(msg);

        //4.修改pet的成本价
        Pet pet = petMapper.findById(order.getPetId());
        pet.setCostprice(orderDto.getMoney());
        petMapper.update(pet);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        //修改订单状态：-1
        OrderPetAcquisition order = orderPetAcquisitionMapper.findById(id);
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
        //修改寻主消息 = 放到寻主池【state=3，shopId=null】
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());
        //删除店铺id
        petMapper.delete(order.getPetId());
    }
}
